package helpdesk;

import java.util.ArrayList;
import javax.swing.table.AbstractTableModel;
/**
 *
 * @author MattLampe
 */
public class ViewDocGUI extends javax.swing.JFrame
{
    ArrayList<FileRec> data;
    String collection;
    
    /**
     * Creates new form ViewDocGUI
     */
    public ViewDocGUI(ArrayList<FileRec> fileList, String collect)
    {
        //This gathers from the database a list of uploaded documents and displays
        //them in a table 
        data = fileList;
        collection = collect;
        initComponents();
        
        docTable.getColumnModel().getColumn(0).setHeaderValue("Filename");
        docTable.getColumnModel().getColumn(1).setHeaderValue("Acknowledgement");
        
        FileRec temp;
        
        for(int i = 0; i < data.size(); i++)
        {
            temp = data.get(i);
            docTable.setValueAt(temp.filename, i, 0);
            docTable.setValueAt(Boolean.toString(temp.read), i, 1);
        }
        setVisible(true);
    }

    private ViewDocGUI()
    {
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        docTable = new javax.swing.JTable();
        viewFileButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Lucida Grande", 0, 24)); // NOI18N
        jLabel1.setText("Uploaded Documents");

        docTable.setModel(new AbstractTableModel()
            {
                @Override
                public int getRowCount()
                {
                    return data.size();
                }

                @Override
                public int getColumnCount()
                {
                    return 2;
                }

                @Override
                public String getValueAt(int i, int j)
                {
                    if (j == 0)
                    {
                        return data.get(i).filename;
                    }
                    else
                    {
                        return Boolean.toString(data.get(i).read);
                    }
                }

                public void setValueAt(String val, int i, int j)
                {
                    if (val == "true")
                    {
                        data.set(i, new FileRec(data.get(i).filename, true));
                    }
                    else if(val == "false")
                    {
                        data.set(i, new FileRec(data.get(i).filename, false));
                    }
                    else
                    {
                        data.set(i, new FileRec(val, data.get(i).read));
                    }
                }
            });
            jScrollPane1.setViewportView(docTable);

            viewFileButton.setText("View File");
            viewFileButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    viewFileButtonActionPerformed(evt);
                }
            });

            cancelButton.setText("Cancel");
            cancelButton.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cancelButtonActionPerformed(evt);
                }
            });

            org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                        .add(layout.createSequentialGroup()
                            .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 243, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                                .add(jLabel1))
                            .add(0, 0, Short.MAX_VALUE))
                        .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                            .add(0, 0, Short.MAX_VALUE)
                            .add(viewFileButton)
                            .add(18, 18, 18)
                            .add(cancelButton)))
                    .addContainerGap())
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                .add(layout.createSequentialGroup()
                    .addContainerGap()
                    .add(jLabel1)
                    .add(18, 18, 18)
                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 148, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(18, 18, 18)
                    .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                        .add(viewFileButton)
                        .add(cancelButton))
                    .addContainerGap(21, Short.MAX_VALUE))
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents
    //This button downloads and views the document selected
    private void viewFileButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_viewFileButtonActionPerformed
        if (docTable.getSelectedRow() >= 0)
        {
            GFS.retrieveFile(data.get(docTable.getSelectedRow()).filename, collection, MongoDB.getConnection());
        }
    }//GEN-LAST:event_viewFileButtonActionPerformed
    //This button closes the window
    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        this.dispose();
    }//GEN-LAST:event_cancelButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ViewDocGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ViewDocGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ViewDocGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ViewDocGUI.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ViewDocGUI().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JTable docTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JButton viewFileButton;
    // End of variables declaration//GEN-END:variables
}
